#include "util.hpp"
#include "config.hpp"
#include "data.hpp"
#include "hotmanager.hpp"
#include "service.hpp"

void FileUtilTest1(const std::string &filename) {
    clx_cloud::FileUtil file(filename);
    std::cout << file.FileSize() << std::endl;
    std::cout << file.FileName() << std::endl;
    std::cout << file.LastMTime() << std::endl;
    std::cout << file.LastATime() << std::endl;
    return;
}

void FileUtilTest2(const std::string &filename) {
    clx_cloud::FileUtil file(filename);
    std::string body;
    file.GetContent(&body);
    clx_cloud::FileUtil new_file("./hello.txt");
    new_file.SetContent(&body);
    return;
}

void FileUtilTest3(const std::string &filename) {
    clx_cloud::FileUtil file(filename);
    file.Compress(filename + ".lz");
    clx_cloud::FileUtil new_lz(filename + ".lz");
    file.UnCompress(filename + "2");
    return;
}

void FileUtilTest4(const std::string &filename) {
    clx_cloud::FileUtil fu(filename);
    fu.CreateDirectory();
    std::vector<std::string> array;
    fu.ScanDirectory(&array);
    for (auto & a : array) {
        std::cout << a << std::endl;
    }
    return;
}

void JsonUtilTest1(){
    const char *name = "xiao ming";
    int age = 19;
    float score[] = {85, 88.5, 99};
    Json::Value root;
    root["姓名"] = name;
    root["年龄"] = age;
    root["成绩"].append(score[0]);
    root["成绩"].append(score[1]);
    root["成绩"].append(score[2]);
    std::string serialize_str;
    clx_cloud::JsonUtil json_str;
    clx_cloud::JsonUtil::Serialize(root, &serialize_str);
    std::cout << serialize_str << std::endl;

    Json::Value val;
    clx_cloud::JsonUtil::UnSerialize(serialize_str, &val);
    std::cout << "姓名 : " << val["姓名"] << std::endl;
    std::cout << "年龄 : " << val["年龄"] << std::endl;
    std::cout << "成绩 : " << val["成绩"][0] << std::endl;
    std::cout << "成绩 : " << val["成绩"][1] << std::endl;
    std::cout << "成绩 : " << val["成绩"][2] << std::endl;
}

void ConfigTest1() {
    std::shared_ptr<clx_cloud::Config> config(clx_cloud::Config::GetInstance());
    std::cout << config->GetHotTime() << std::endl;
    std::cout << config->GetServerPort() << std::endl;
    std::cout << config->GetServerIp() << std::endl;
    std::cout << config->GetDownloadPrefix() << std::endl;
    std::cout << config->GetPackFileSuffix() << std::endl;
    std::cout << config->GetBackDir() << std::endl;
    std::cout << config->GetBackupFile() << std::endl;
}

void DataTest1(const std::string &filename) {
    clx_cloud::BackupInfo info;
    info.NewBackupInfo(filename);
    std::cout << info.pack_flag << std::endl;
    std::cout << info.fsize << std::endl;
    std::cout << info.atime << std::endl;
    std::cout << info.mtime << std::endl;
    std::cout << info.real_path << std::endl;
    std::cout << info.pack_path << std::endl;
    std::cout << info.url_path  << std::endl;
}

void DataTest2(const std::string &filename) {
    clx_cloud::BackupInfo info;
    info.NewBackupInfo(filename);
    clx_cloud::DataManager data;
    data.Insert(info);

    clx_cloud::BackupInfo tmp;
    data.GetOneByUrl("/download/bundle.h", &tmp);

    std::cout << tmp.pack_flag << std::endl;
    std::cout << tmp.fsize << std::endl;
    std::cout << tmp.atime << std::endl;
    std::cout << tmp.mtime << std::endl;
    std::cout << tmp.real_path << std::endl;
    std::cout << tmp.pack_path << std::endl;
    std::cout << tmp.url_path  << std::endl;

    info.pack_flag = true;
    data.Update(info);
    std::vector<clx_cloud::BackupInfo> array;
    data.GetAll(&array);
    for (auto &a : array) {
        std::cout << a.pack_flag << std::endl;
        std::cout << a.fsize << std::endl;
        std::cout << a.atime << std::endl;
        std::cout << a.mtime << std::endl;
        std::cout << a.real_path << std::endl;
        std::cout << a.pack_path << std::endl;
        std::cout << a.url_path  << std::endl;
    }

    data.GetOneByRealpath(filename, &tmp);
    std::cout << tmp.pack_flag << std::endl;
    std::cout << tmp.fsize << std::endl;
    std::cout << tmp.atime << std::endl;
    std::cout << tmp.mtime << std::endl;
    std::cout << tmp.real_path << std::endl;
    std::cout << tmp.pack_path << std::endl;
    std::cout << tmp.url_path  << std::endl;
}

void DataTest3() {
    clx_cloud::DataManager data;
    std::vector<clx_cloud::BackupInfo> array;
    data.GetAll(&array);
    for (auto &a : array) {
        std::cout << a.pack_flag << std::endl;
        std::cout << a.fsize << std::endl;
        std::cout << a.atime << std::endl;
        std::cout << a.mtime << std::endl;
        std::cout << a.real_path << std::endl;
        std::cout << a.pack_path << std::endl;
        std::cout << a.url_path  << std::endl;
    }
}
clx_cloud::DataManager *_data;
void HotManagerTest1() {
    // _data = new clx_cloud::DataManager(); 单独测试时使用
    clx_cloud::HotManager hot;
    hot.RunModule();
}

void ServiceUploadTest1() {
    // _data = new clx_cloud::DataManager(); // 单独测试时使用
    clx_cloud::Service srv;
    srv.RunModule();
}

void Server() {
    _data = new clx_cloud::DataManager();
    std::thread thread_hot_manager(HotManagerTest1);
    std::thread thread_service(ServiceUploadTest1);
    thread_hot_manager.join();
    thread_service.join();
}

int main(int argc, char* argv[]) {
    // FileUtilTest1(argv[1]);
    // FileUtilTest2(argv[1]);
    // FileUtilTest3(argv[1]);
    // FileUtilTest4(argv[1]);
    // JsonUtilTest1();
    // ConfigTest1();
    // DataTest1(argv[1]);
    // DataTest3();
    // HotManagerTest1();
    // ServiceUploadTest1();
    Server();
    return 0;
}